package hk.mrtomato.help;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tiles.TilesContainer;
import org.apache.tiles.servlet.context.ServletUtil;

public class ActionForward {

	private String url;
	private boolean isTiles=true;
	private boolean isRedirect=false;
	
	public boolean isTiles() {
		return isTiles;
	}
	public void setTiles(boolean isTiles) {
		this.isTiles = isTiles;
	}
	public ActionForward() {		
		url="";
		isRedirect = false;
	}
	public ActionForward(String url, boolean isRed) {
		this.url = url;
		this.isRedirect = isRed;
	}
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public void go(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		if(isTiles){
			tilesGO(getUrl(),request,response);
		}else{
			redirect(request,response);
		}
	}
	
	private void tilesGO(String tname,HttpServletRequest request,HttpServletResponse response){
		
		TilesContainer container = ServletUtil.getContainer(
		        request.getSession().getServletContext());
		container.render(tname, request,response);
	}
	
	private void redirect(HttpServletRequest request, HttpServletResponse response){
		try {
			response.sendRedirect(this.getUrl());
		} catch (IOException e) {
			System.out.println("redirect : "+e);
		}
	}
	
}
